package org.credo.net;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;

import javax.enterprise.context.RequestScoped;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.apache.commons.io.IOUtils;

/**
 * @DES:这个是模仿MBOSS方的接收响应.以及Operan发送。
 * @author: Credo
 * @date: 2012-12-17
 */
@Path("/test")
@RequestScoped
public class TestimitateMBOSSPostMethod {
	/*
	 * =======================下面是模仿MBOSS方的接收响应====================================
	 */
	@POST
	@Consumes("*/*")
	public Response testMessageReceive(InputStream message) throws IOException {
		System.out.println("进入MBOSS接收方法");

		String xml = IOUtils.toString(message);
		// System.out.println("接收到的XML为:");
		System.out.println(xml.substring(0, 100));
		System.out.println("=====================其他处理============================");
		// 读取要返回的XML文件
		String xmlpath = "D:\\restxml\\succ.xml";
		// xml文件保存路径
		BufferedReader fin = new BufferedReader(new FileReader(xmlpath));
		StringBuffer xmlBuffer = new StringBuffer();
		int k = 0;
		while (k != -1) {
			k = fin.read();
			if (k != -1) {
				xmlBuffer.append((char) k);
			}
		}
		fin.close();
		String xmlStr = xmlBuffer.toString();
		return Response.status(Status.OK).entity(xmlStr.getBytes()).build();
	}

	/*
	 * =======================下面是模仿Operan的数据发送====================================
	 */
	// 下面是模仿Operan的数据发送
	public static void main(String[] args) {
		System.out.println("开始main方法:");
		TestimitateMBOSSPostMethod test = new TestimitateMBOSSPostMethod();
		test.sendPostToRest();
	}

	public void sendPostToRest() {
		try {
			String xmlpath = "D:\\restxml\\stay.xml";// xml文件保存路径
			// 读取xml文件
			BufferedReader fin = new BufferedReader(new FileReader(xmlpath));
			StringBuffer xmlBuffer = new StringBuffer();
			int k = 0;
			while (k != -1) {
				k = fin.read();
				if (k != -1) {
					xmlBuffer.append((char) k);
				}
			}
			fin.close();
			System.out.println("XML内容为:" + xmlBuffer.toString());
			System.out.println("发送xml文件");
			String xmlStr = xmlBuffer.toString();
			// String url2 =
			// "http://localhost:8080/kunlun-integration/rest/OXIInterface?propertyName=credoTest&messageType=RATE&status=123";
			String url2 = "http://localhost:8080/kunlun-integration/rest/OXIInterface?propertyName=credoTest&messageType=STAY&status=123";
			java.net.URL pageUrl = new java.net.URL(url2);
			HttpURLConnection httpurlconnection = (HttpURLConnection) pageUrl.openConnection();
			httpurlconnection.setRequestMethod("POST");
			httpurlconnection.setDoInput(true);
			httpurlconnection.setDoOutput(true);

			httpurlconnection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
			httpurlconnection.setRequestProperty("Cache-Control", "no-cache");
			httpurlconnection.setRequestProperty("Pragma", "no-cache");
			httpurlconnection.setRequestProperty("Expires", "-1");

			httpurlconnection.setRequestProperty("Content-Length", "" + xmlStr.getBytes().length);
			System.out.println("lengthNum:" + xmlStr.getBytes().length);

			httpurlconnection.getOutputStream().write(xmlStr.getBytes());

			httpurlconnection.getOutputStream().flush();
			httpurlconnection.getOutputStream().close();
			BufferedInputStream bis = new BufferedInputStream(httpurlconnection.getInputStream());
			bis.close();
			xmlBuffer = null;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
